package net.lanelife.casino.caipiao.utils;

import java.util.List;

import net.lanelife.casino.caipiao.entity.RebatePointLevel;
import net.lanelife.casino.caipiao.entity.User;
import net.lanelife.casino.common.utils.StringUtils;

import com.google.common.collect.Lists;

public class RebatePointUtils {
	
	public static final String DIRECTION_IN = "IN";
	public static final String DIRECTION_OUT = "OUT";
	
	/**
	 * 生成新的配额，去掉与返点等级表不匹配的返点配额
	 * @param levels   返点等级列表
	 * @param oriQuota 原始配额信息
	 * @param point    用户当前返点
	 * @return
	 */
	public static String generateQuota(List<RebatePointLevel> levels, String oriQuota, Double point) {
		oriQuota = oriQuota == null ? "" : oriQuota;
		StringBuilder sb = new StringBuilder();
		String[] quotas = oriQuota.split("\\|");
		for (RebatePointLevel level : levels) {
			if (level.getBeginRebatePoint() > point) break;
			String lv = level.getBeginRebatePoint() + "-" + level.getEndRebatePoint();
			if (oriQuota.contains(lv)) {
				for (String quota : quotas) {
					if (quota.contains(lv)) {
						sb.append(quota);				// 匹配，保留
						break;
					}
				}
			} else {
				sb.append(lv).append(":").append(0);	// 不匹配，配额为0
			}
			sb.append("|");
		}
		return sb.toString();
	}
	
	/**
	 * 比较配额
	 * @param curQuota 当前配额信息
	 * @param oriQuota 原始配额信息
	 * @return 返回原始配额在当前配额中不存在的配额
	 */
	public static List<User.Quota> compareQuota(String curQuota, String oriQuota) {
		List<User.Quota> list = Lists.newArrayList();
		if (StringUtils.isNotBlank(oriQuota)) {
			String[] o = oriQuota.split("\\|");
			for (String oq : o) {
				String[] arr = oq.split(":");
				String op = arr[0];
				if (!curQuota.contains(op)) {
					String[] ops = op.split("-");
					list.add(new User.Quota(Double.valueOf(ops[0]), Double.valueOf(ops[1]), Long.valueOf(arr[1])));
				}
			}
		}
		return list;
	}
	
	/**
	 * 增减配额
	 * @param oriQuota 原始配额信息
	 * @param point    返点
	 * @param quota    配额
	 * @param dir      方向，IN：增加；OUT：扣除
	 * @return
	 */
	public static String changeQuota(String oriQuota, Double point, Integer quota, String dir) {
		StringBuilder sb = new StringBuilder();
		String[] quotas = oriQuota.split("\\|");
		for (String q : quotas) {
			String[] a1 = q.split(":");
			String[] a2 = a1[0].split("-");
			double begin = Double.valueOf(a2[0]); // 起返点
			double end = Double.valueOf(a2[1]);   // 止返点
			int oq = Integer.valueOf(a1[1]);      // 原有配额
			if (begin <= point && end >= point) {
				int nq = DIRECTION_IN.equals(dir) ? oq + quota : oq - quota;
				sb.append(a1[0]).append(":").append(nq);
			} else {
				sb.append(q);
			}
			sb.append("|");
		}
		return sb.toString();
	}
	
	/**
	 * 获取配额
	 * @param quota 配额信息
	 * @param point 返点
	 * @return -1代表找不到该返点级别
	 */
	public static Integer getQuota(String quota, Double point) {
		String[] quotas = quota.split("\\|");
		for (String q : quotas) {
			String[] a1 = q.split(":");
			String[] a2 = a1[0].split("-");
			double begin = Double.valueOf(a2[0]); // 起返点
			double end = Double.valueOf(a2[1]);   // 止返点
			int oq = Integer.valueOf(a1[1]);      // 配额
			if (begin <= point && end >= point) {
				return oq;
			}
		}
		return -1;
	}
	
	/**
	 * 获取返点级别
	 * @param levels
	 * @param point
	 * @return
	 */
	public static RebatePointLevel getLevel(List<RebatePointLevel> levels, Double point) {
		for (RebatePointLevel level : levels) {
			if (level.getBeginRebatePoint() <= point && level.getEndRebatePoint() >= point) {
				return level;
			}
		}
		return null;
	}

}
